<?php

namespace api\modules\backend\controllers;

use yii;
use yii\filters\auth\HttpBearerAuth;
use yii\web\UploadedFile;
use common\commands\Common;
use api\modules\backend\models\UploadForm;


/**
 * Upload Controller API
 *
 * @author Fong <xiaofeng.zhang@suppernovachina.com>
 */
class UploadController extends ControllerBase
{
    public $modelClass = 'api\modules\backend\models\Upload';
    public $uploadFormModel;

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => HttpBearerAuth::className(),
            'optional' => [
                'file'
            ],
        ];
        $behaviors['authenticator']['except'] = ['options'];

        return $behaviors;
    }

    /**
     * 上传文件
     */
    public function actionFile()
    {
        $prefix = [];
        $type = Yii::$app->request->post('type');

        if (!$type) {
            $this->response(Common::getPackedResponse(MISSING_PARAMS, Yii::t('app', 'MISSING_PARAMS')));
        }

        if ($type !== 'video' && $type !== 'image') {
            $this->response(Common::getPackedResponse(MISSING_PARAMS, Yii::t('app', 'ERROR_PARAMS')));
        }

        $this->uploadFormModel = new UploadForm();
        $this->uploadFormModel->setTargetFileType($type);

        if (Yii::$app->request->isPost) {
            $this->uploadFormModel->file = UploadedFile::getInstances($this->uploadFormModel, 'file');

            $thumbArr = [];
            if ($this->uploadFormModel->file && $this->uploadFormModel->validate()) {

                foreach ($this->uploadFormModel->file as $file) {
                    $folder = 'uploads/' . date("Y") . '/' . date("m") . '/' . date("d");

                    $extension = $this->uploadFormModel->getUploadFileExtension($file->type);
                    $filename = $this->uploadFormModel->generateRandomName(16) . $extension;
                    $this->uploadFormModel->fileExists(Yii::$app->basePath . '/web', $folder);

                    $path = $folder . '/' . time() . $filename;

                    $file->saveAs($path);

                    switch ($extension) {
                        case '.zip':
                            $thumbArr = $this->uploadFormModel->unZip($path, $folder);
                            $prefix = $this->uploadFormModel->getPrefix();
                            break;
                        case '.mp4':
                            $resource[] = [
                                'filename' => $filename,
                                'path' => $path
                            ];
                            $thumbArr = $this->uploadFormModel->generateImage($resource, $folder);
                            break;
                        default:
                            $thumbArr[] = [
                                'filename' => $filename,
                                'path' => $path
                            ];
                    }
                }

                if (!empty($thumbArr)) {
                    $data = array('thumb' => $thumbArr, 'prefix' => $prefix);
                    $this->success($data);
                }
            }
        }

        $this->response(Common::getPackedResponse(DATABASE_FAILED));
    }
}